package modules.cms.models
{
	import flash.utils.getQualifiedClassName;
	import devdoo.fx.UseDB;
	import devdoo.fx.UseUtil;
	import devdoo.fx.UseString;
	import devdoo.fx.interfaces.ITableModel;
	
	public class UseMenuSubcategoryLanguage implements ITableModel
	{
		private var _classNameModel:String = 'useMenuSubcategoryLanguage';
		private var _table:String = 'menu_subcategory_language';
		private var _alias:String = 'USE_MENU_SUBCATEGORY_LANGUAGE';
		private var _data:Object = new Object();
		private var _fields:Object = new Object();
		private var _idField:String = 'id';
		private var _idFieldAlias:String = 'USE_MENU_SUBCATEGORY_LANGUAGE_id';
		private var _useTableAlias:Boolean;
		private var _isSave:Boolean = false;
		
		public function UseMenuSubcategoryLanguage(){
			this._fields.USE_MENU_SUBCATEGORY_LANGUAGE_href_title = "href_title";
			this._fields.USE_MENU_SUBCATEGORY_LANGUAGE_id = "id";
			this._fields.USE_MENU_SUBCATEGORY_LANGUAGE_language = "language";
			this._fields.USE_MENU_SUBCATEGORY_LANGUAGE_menu_subcategory = "menu_subcategory";
			this._fields.USE_MENU_SUBCATEGORY_LANGUAGE_metatag_content = "metatag_content";
			this._fields.USE_MENU_SUBCATEGORY_LANGUAGE_metatag_description = "metatag_description";
			this._fields.USE_MENU_SUBCATEGORY_LANGUAGE_metatag_keywords = "metatag_keywords";
			this._fields.USE_MENU_SUBCATEGORY_LANGUAGE_title = "title";
			this._fields.USE_MENU_SUBCATEGORY_LANGUAGE_url_title_id = "url_title_id";
		}
		
		public function get tableAlias():Object{
			var table:Object = new Object();
			table[this._alias] = this._table;
			return table;
		}
		
		public function get classNameModel():String{
			return this._classNameModel;
		}
		
		public function get table():String{
			return this._table;
		}
		
		public function get idField():String{
			return this._idField;
		}
		
		public function get idFieldAlias():String{
			return this._idFieldAlias;
		}
		
		public function get alias():String{ return this._alias; }
		
		public function fields(fieldsList:Array, useTableAlias:Boolean=false):Object{
			var data:Object = new Object();
			var fields:Object = this._fields;
			var errorFields:String = '';
			var error:Boolean = true;
			var item:String;
			var field:String;
			var i:String;
			var j:String;
			
			this._useTableAlias = useTableAlias;
			if(typeof(fieldsList[0]) == 'string' && fieldsList[0] == '*' ){
				if(useTableAlias){
					for ( field in this._fields){
						data[field] = this._fields[field];
					}
				}
				else{
					for ( field in this._fields){
						data[(this._fields[field])] = this._fields[field];
					}
				}
				for (item in this._setVar){
					data[item] = this._setVar[item];
				}
				for (item in this._expr){
					data[item] = this._expr[item];
				}
				return data;
			}
			for (j in fieldsList){
				if(useTableAlias){
					for (i in this._fields){
						if( fields[i] == fieldsList[j] ){
							data[this._alias+'_'+fields[i]] = fieldsList[j];
							error = false;
							continue;
						}
					}
				}
				else{
					for (i in fields){
						if(typeof(fieldsList[j])== 'object'){
							for (var k:String in fieldsList[j]) {
								data[k] = fieldsList[j][k];
								error = false;
								continue;
							}
						}
						else if( fields[i] == fieldsList[j] ){
							data[(fields[i])] = fieldsList[j];
							error = false;
							continue;
						}
					}
				}
				if(error) errorFields += fieldsList[j]+' :: ';
				error = true;
			}
			if(errorFields != ''){
				throw new Error(getQualifiedClassName(this) +'::Error( :: '+errorFields+')', 'Error :: Campos não encontrado no modelo da tabela '+this._table);
			}
			for (item in this._setVar){
				data[item] = this._setVar[item];
			}
			for (item in this._expr){
				data[item] = this._expr[item];
			}
			return data;
		}
		
		private var _expr:Object = new Object();
		public function expr(field:String, expr:String, alias:String = null, useTableAlias:Boolean=false):UseMenuSubcategoryLanguage{
			var fields:Object = this._fields;
			var error:Boolean = true;
			for (var i:String in fields){
				if( fields[i] == field ) error = false;
			}
			if(error){
				throw new Error(getQualifiedClassName(this) +'::Error exp('+field+')', 'Error :: Campo utilizado em exp(?) não encontrado no modelo da tabela '+this._table);
				return null;
			}
			if(alias == null){
				if(useTableAlias)
					alias = this._alias + '_' + field;
				else
					alias = field;
			}
			else{
				if(useTableAlias)
					alias =  this._alias + '_' + alias;
			}
			this._expr[(alias)] = UseString.replace(expr, '?', this._alias + '.' + field) +' as ' + alias;
			return this;
		}
		
		private var _setVar:Object = new Object();
		public function setVar(field:String, value:String):UseMenuSubcategoryLanguage{
			this._setVar[field] = value;
			return this;
		}
		public function get dataAlias():Object{
			var data:Object = {};
			data.USE_MENU_SUBCATEGORY_LANGUAGE_href_title = this.href_title;
			data.USE_MENU_SUBCATEGORY_LANGUAGE_id = this.id;
			data.USE_MENU_SUBCATEGORY_LANGUAGE_language = this.language;
			data.USE_MENU_SUBCATEGORY_LANGUAGE_menu_subcategory = this.menu_subcategory;
			data.USE_MENU_SUBCATEGORY_LANGUAGE_metatag_content = this.metatag_content;
			data.USE_MENU_SUBCATEGORY_LANGUAGE_metatag_description = this.metatag_description;
			data.USE_MENU_SUBCATEGORY_LANGUAGE_metatag_keywords = this.metatag_keywords;
			data.USE_MENU_SUBCATEGORY_LANGUAGE_title = this.title;
			data.USE_MENU_SUBCATEGORY_LANGUAGE_url_title_id = this.url_title_id;
			return data;
		}
		public function get data():Object{
			var data:Object = {};
			data.href_title = this.href_title;
			data.id = this.id;
			data.language = this.language;
			data.menu_subcategory = this.menu_subcategory;
			data.metatag_content = this.metatag_content;
			data.metatag_description = this.metatag_description;
			data.metatag_keywords = this.metatag_keywords;
			data.title = this.title;
			data.url_title_id = this.url_title_id;
			return data;
		}
		public function set data(data:Object):void{
			var len:int = this._alias.length+1;
			var alias:String;
			var tempIndex:String;
			for (var index:* in data){
				alias = index.substring(0,len);
				if(alias == this._alias+'_'){
					tempIndex = index.replace(alias,'');
				}
				if(this.hasOwnProperty(tempIndex)){
					this[tempIndex] = data[index];
				}
			}
		}
		
		public function find(id:int, callBack:Function):void{
			var useDB:UseDB = new UseDB('SELECT');
			useDB.setFetchMode(UseDB.FETCH_OBJ)
				.from(tableAlias)
				.fields(['*'])
				.where(this._alias +'.'+ this.idField + ' = ?', String(id))
				.fetch('All')
				.connect(callBack);
		}
		
		public function remove(id:int, callBack:Function):void{
			var useDB:UseDB = new UseDB('DELETE');
			useDB.setFetchMode(UseDB.FETCH_OBJ)
			useDB.table(table)
				.where(this._alias +'.'+ this.idField + ' = ?', String(id))
				.connect(callBack);
		}
		
		public function update(callBack:Function):void{
			var useDB:UseDB = new UseDB('UPDATE');
			useDB.table(table)
				.data(this._data)
				.where(this.idField + ' = ?', this.id)
				.connect(callBack);
		}
		
		public function insert(callBack:Function):void{
			var useDB:UseDB = new UseDB('INSERT');
			useDB.table(table)
				.data(this._data)
				.connect(callBack);
		}
		
		public function fetch(callBack:Function, type:String = 'All'):void{
			var useDB:UseDB = new UseDB('SELECT');
			useDB.from(tableAlias)
				.fields(['*'])
				.fetch(type)
				.connect(callBack);
		}
		
		private var _href_title:*;
		public function set href_title(href_title:*):void{
			this._href_title = UseUtil.checkSetData('href_title', href_title);
			this._data['href_title'] = href_title;
			this._isSave = true;
		}
		public function get href_title():*{ return this._href_title; }
		
		private var _id:*;
		public function set id(id:*):void{
			this._id = UseUtil.checkSetData('id', id);
			this._data['id'] = id;
			this._isSave = true;
		}
		public function get id():*{ return this._id; }
		
		private var _language:*;
		public function set language(language:*):void{
			this._language = UseUtil.checkSetData('language', language);
			this._data['language'] = language;
			this._isSave = true;
		}
		public function get language():*{ return this._language; }
		
		private var _menu_subcategory:*;
		public function set menu_subcategory(menu_subcategory:*):void{
			this._menu_subcategory = UseUtil.checkSetData('menu_subcategory', menu_subcategory);
			this._data['menu_subcategory'] = menu_subcategory;
			this._isSave = true;
		}
		public function get menu_subcategory():*{ return this._menu_subcategory; }
		
		private var _metatag_content:*;
		public function set metatag_content(metatag_content:*):void{
			this._metatag_content = UseUtil.checkSetData('metatag_content', metatag_content);
			this._data['metatag_content'] = metatag_content;
			this._isSave = true;
		}
		public function get metatag_content():*{ return this._metatag_content; }
		
		private var _metatag_description:*;
		public function set metatag_description(metatag_description:*):void{
			this._metatag_description = UseUtil.checkSetData('metatag_description', metatag_description);
			this._data['metatag_description'] = metatag_description;
			this._isSave = true;
		}
		public function get metatag_description():*{ return this._metatag_description; }
		
		private var _metatag_keywords:*;
		public function set metatag_keywords(metatag_keywords:*):void{
			this._metatag_keywords = UseUtil.checkSetData('metatag_keywords', metatag_keywords);
			this._data['metatag_keywords'] = metatag_keywords;
			this._isSave = true;
		}
		public function get metatag_keywords():*{ return this._metatag_keywords; }
		
		private var _title:*;
		public function set title(title:*):void{
			this._title = UseUtil.checkSetData('title', title);
			this._data['title'] = title;
			this._isSave = true;
		}
		public function get title():*{ return this._title; }
		
		private var _url_title_id:*;
		public function set url_title_id(url_title_id:*):void{
			this._url_title_id = UseUtil.checkSetData('url_title_id', url_title_id);
			this._data['url_title_id'] = url_title_id;
			this._isSave = true;
		}
		public function get url_title_id():*{ return this._url_title_id; }
		
		public function get isSave():Boolean{ return this._isSave; }
		public function get useTableAlias():Boolean{ return this._useTableAlias; }
	}
}

